package com.sfeir.client.activity;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.media.client.Audio;
import com.google.gwt.user.client.ui.Image;
import com.google.web.bindery.event.shared.EventBus;
import com.sfeir.canvas.activities.client.AbstractCanvasActivity;
import com.sfeir.canvas.activities.client.event.PageChangeEvent;
import com.sfeir.canvas.util.client.ResourceManager;
import com.sfeir.client.i18n.TetrisConstants;
import com.sfeir.client.util.Constants;

/**
 * This is the setting activity allowing to change and handle the game global
 * setting, like the enabling/disabling of the sound ...
 * 
 * @author Oussama ZOGHLAMI
 * @date 06/01/2012
 */
public class SettingsActivity extends AbstractCanvasActivity {

	private static final int BACK_BUTTON_X = 100;
	private static final int BACK_BUTTON_Y = 450;
	private static final int BACK_BUTTON_SIZE = 64;
	private static final int SOUND_BUTTON_X = 180;
	private static final int SOUND_BUTTON_Y = 200;
	private static final int SOUND_BUTTON_SIZE = 74;

	private Context2d ctx;
	private final Image panel;
	private final Image back;
	private final Image background;
	private EventBus bus;
	private final TetrisConstants texts;
	private Image soundOnImage;
	private Image soundOffImage;
	private boolean isSoundActivated = true;
	private ResourceManager resourceManager;

	public SettingsActivity(ResourceManager manager) {
		this.panel = manager.getImage(Constants.KEY_IMAGE_PANEL);
		this.back = manager.getImage(Constants.KEY_IMAGE_BACK);
		this.background = manager.getImage(Constants.KEY_IMAGE_BACKGROUND);
		this.texts = GWT.create(TetrisConstants.class);
		this.soundOnImage = manager.getImage(Constants.KEY_IMAGE_SOUND_ON);
		this.soundOffImage = manager.getImage(Constants.KEY_IMAGE_SOUND_OFF);
		this.resourceManager = manager;
	}

	@Override
	public void start(Canvas canvas, EventBus bus) {
		this.bus = bus;
		this.ctx = canvas.getContext2d();
		this.ctx.drawImage(ImageElement.as(background.getElement()), 0, 0);
		this.ctx.drawImage(ImageElement.as(panel.getElement()), 0, 0);
		this.ctx.drawImage(ImageElement.as(back.getElement()), BACK_BUTTON_X, BACK_BUTTON_Y);
		this.ctx.save();

		this.ctx.setFont("bold 30pt calibri");
		this.ctx.fillText(texts.settings(), 300, 120);

		if (isSoundActivated) {
			this.ctx.drawImage(ImageElement.as(this.soundOnImage.getElement()), SOUND_BUTTON_X, SOUND_BUTTON_Y);
		} else {
			this.ctx.drawImage(ImageElement.as(this.soundOffImage.getElement()), SOUND_BUTTON_X, SOUND_BUTTON_Y);
		}
		this.ctx.setFont("16pt calibri");
		this.ctx.fillText(texts.soundEnableDisable(), SOUND_BUTTON_X + 80, SOUND_BUTTON_Y + 38);

		this.ctx.restore();

	}

	@Override
	public void onClick(ClickEvent event) {
		int x = event.getX();
		int y = event.getY();

		// Handle the click on the back image
		if (x >= BACK_BUTTON_X && x <= BACK_BUTTON_X + BACK_BUTTON_SIZE && y >= BACK_BUTTON_Y
				&& y <= BACK_BUTTON_Y + BACK_BUTTON_SIZE) {
			this.bus.fireEvent(new PageChangeEvent(PageChangeEvent.BACK, null));
		}

		// Handle the click on the sound option image
		if (x >= SOUND_BUTTON_X && x <= SOUND_BUTTON_X + SOUND_BUTTON_SIZE && y >= SOUND_BUTTON_Y
				&& y <= SOUND_BUTTON_Y + SOUND_BUTTON_SIZE) {
			switchSoundOption();
		}
	}

	/**
	 * Method allowing to switch the sound option
	 */
	private void switchSoundOption() {
		this.isSoundActivated = !this.isSoundActivated;
		Image image = isSoundActivated ? this.soundOnImage : this.soundOffImage;
		this.ctx.drawImage(ImageElement.as(image.getElement()), SOUND_BUTTON_X, SOUND_BUTTON_Y);
		this.resourceManager.modifAudio(new ResourceManager.ObjectMofier<Audio>() {
			@Override
			public void modify(Audio object) {
				object.setMuted(isSoundActivated);
			}
		});
	}

}
